package com.inspur.demo.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义Route拦截器
 */
@Component
public class CustomerRouteFilter extends ZuulFilter {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String filterType() {
        return "route";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        int responseStatusCode = currentContext.getResponseStatusCode();
        return responseStatusCode != 401;
    }

    @Override
    public Object run() throws ZuulException {
        logger.info("--> CustomerRouteFilter <--");
        RequestContext context = RequestContext.getCurrentContext();
        Object user = context.get("user");
        if (user != null) {
            logger.info(String.format("当前登录用户：%s", user.toString()));
        }
        HttpServletRequest request = context.getRequest();
        String requestURI = request.getRequestURI();
        logger.info(requestURI);
        return null;
    }
}
